﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO;
using System.Collections;
using System.Net;
using System.Drawing.Text;
using System.Drawing;

namespace vxi.chat.library
{
    /// <summary> 
    /// 显示跟踪消息的事件委托 
    /// </summary> 
    public delegate void WriteLogHandle(string msg,LogType type);

    /// <summary> 
    /// 显示数量统计的事件委托 
    /// </summary> 
    public delegate void QuantityHandle(int num1,int num2);

    /// <summary>
    /// 日志消息等级
    /// </summary>
    public enum LogType
    { 
        Sql=4,       //sql
        Exception= 3,  //异常
        Event=2,     //事件
        General=1    //一般
    }

    public class ChatLog
    {
        public string log { get; set; }
        public LogType type { get; set; }
        public ChatLog(string _log,LogType _type)
        {
            log = _log;
            type = _type;
        }
    }

    public static class VxiGlobals
    {


        public static List<string> Families
        {
            get
            {
                InstalledFontCollection font = new InstalledFontCollection();
                List<string> families = new List<string>();
                foreach (FontFamily familiy in font.Families)
                {
                    if (familiy.IsStyleAvailable(FontStyle.Regular) &&
                        familiy.IsStyleAvailable(FontStyle.Italic) &&
                        familiy.IsStyleAvailable(FontStyle.Bold))
                    {
                        families.Add(familiy.Name);
                    }
                }
                return families;
            }

        }

        public static string ToC2SLogFormat(VxiEvent evt,IPEndPoint point)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("Receive:");
            builder.Append(VxiDefine.Event.Head.DOMAIN).Append("=").Append(evt.head._domain).Append(",");
            builder.Append(VxiDefine.Event.Head.TYPE).Append("=").Append(evt.head._type).Append(",");
            builder.Append(VxiDefine.Event.Head.EVENT).Append("=").Append(evt.head._event).Append(",");
            builder.Append(VxiDefine.Event.Head.REFID).Append("=").Append(evt.head._refid).Append(",");
            foreach (DictionaryEntry de in evt.items)
            {
                builder.Append(de.Key).Append("=").Append(de.Value).Append(",");
            }
            builder.Remove(builder.Length - 1, 1);
            builder.Append("  From:").Append(point.ToString());
            return builder.ToString();
        }

        public static string ToS2CLogFormat(VxiEvent evt, IPEndPoint point)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("Send:");
            builder.Append(VxiDefine.Event.Head.DOMAIN).Append("=").Append(evt.head._domain).Append(",");
            builder.Append(VxiDefine.Event.Head.TYPE).Append("=").Append(evt.head._type).Append(",");
            builder.Append(VxiDefine.Event.Head.EVENT).Append("=").Append(evt.head._event).Append(",");
            builder.Append(VxiDefine.Event.Head.REFID).Append("=").Append(evt.head._refid).Append(",");
            foreach (DictionaryEntry de in evt.items)
            {
                builder.Append(de.Key).Append("=").Append(de.Value).Append(",");
            }
            builder.Remove(builder.Length - 1, 1);
            builder.Append("  To:").Append(point.ToString());
            return builder.ToString();
        }


    }


    /// <summary> 
    /// 序列化反序列化对象 
    /// </summary> 
    public class ObjectSerializer
    {
        public static byte[] Serialize(object obj)
        {
            BinaryFormatter binaryF = new BinaryFormatter();
            MemoryStream ms = new MemoryStream(1024 * 10);
            binaryF.Serialize(ms, obj);
            ms.Seek(0, SeekOrigin.Begin);
            byte[] buffer = new byte[(int)ms.Length];
            ms.Read(buffer, 0, buffer.Length);
            ms.Close();
            return buffer;
        }

        public static object Deserialize(byte[] buffer)
        {
            BinaryFormatter binaryF = new BinaryFormatter();
            MemoryStream ms = new MemoryStream(buffer, 0, buffer.Length, false);
            object obj = binaryF.Deserialize(ms);
            ms.Close();
            return obj;
        }
    }

}
